package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.redis.service.ITravelStratisVoRedisService;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private ITravelStratisVoRedisService travelStratisVoRedisService;

    @Override
    public void save(TravelComment travelComment) {
         travelComment.setId(null);  // （要set null）如果id=“”,Spring-data-MongoDB 不会自动生成id注入 （数据库存在，但是不会将注入参数对象中travelComment）  isPresent
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        // 逻辑严谨
        Optional<TravelComment> optional = travelCommentRepository.findById(id);
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    @Override
    public List<TravelComment> listAll() {
        return travelCommentRepository.findAll();
    }

    @Override
    public void addComment(TravelComment travelComment) {
        travelComment.setCreateTime(new Date());

        // 判断关联的评论id是否为空
        String id = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(id)){
            TravelComment comment = this.get(id);
            travelComment.setRefComment(comment);
        }

        // 评论数+1
        travelStratisVoRedisService.replynumIncrease(travelComment.getTravelId(),1);

        // 保存
        this.save(travelComment);
    }

    @Override
    public List<TravelComment> query(String id) {
        return travelCommentRepository.findByTravelId(id);
    }

    @Override
    public List<TravelComment> getCommentsNew10() {
        Pageable pageable = PageRequest.of(0,10, Sort.Direction.DESC,"createTime");
        List<TravelComment> type = travelCommentRepository.findByType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT, pageable);
        System.out.println(type.size());
        return travelCommentRepository.findByType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT,pageable);
    }
}
